package com.iscas.lndicatormonitor.filter;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Component
public class CustomCorsFilter implements Filter {
    // 受信任的域列表
//    private final List<String> allowedOrigins = Arrays.asList("http://39.104.62.233:8000");

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        String origin = request.getHeader("Origin");
        response.setHeader("Access-Control-Allow-Origin", "*");

//        // 检查来源是否在受信任的列表中
//        if (allowedOrigins.contains(origin)) {
//            response.setHeader("Access-Control-Allow-Origin", origin);
//        }else {
//            response.setHeader("Access-Control-Allow-Origin", null);
//        }
        chain.doFilter(request, response);
    }
}
